<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MY_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('category_model', 'cate');
	}
	/**
	 * 查看栏目
	 * @return [type] [description]
	 */
	public function index() {
		$data['categorys'] = $this->cate->get_category();
		$this->load->view('admin/category.html', $data);
	}

	/**
	 * 添加编辑栏目栏目
	 */
	public function add_cate() {
		$cid = $this->uri->segment(4);
		if (FALSE === $cid) {
			$this->_write();
		} else {
			$this->_edit($cid);
		}
	}

	public function del() {
		$cid = $this->uri->segment(4);
		$this->cate->del($cid);
		redirect('admin/category');

	}

	private function _write() {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('category');

		if (false === $status) {
			$this->load->helper('form');
			$this->load->view('admin/add_cate.html');
		} else {
			$data = array(
				'cname' => $this->input->post('cname')
				);
			$cid = $this->cate->insert($data);
			$this->session->set_flashdata('success', '添加成功');
			redirect('admin/category');
		}
	}

	private function _edit($cid) {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('category');
		if (FALSE === $status) {
			$this->load->helper('form');
			$data['cate'] = $this->cate->get_cate($cid);
			$this->load->view('admin/add_cate.html', $data);
		} else {
			$data = array(
				'cname' => $this->input->post('cname')
				);

			$this->cate->update($cid, $data);
			$this->session->set_flashdata('success', '修改成功');
			redirect('admin/category');
		}
		
	}

}

/* End of file category.php */
/* Location: ./application/controllers/admin/category.php */